package cn.liontalk.concurrent.deadlock;

import java.util.concurrent.atomic.AtomicInteger;

public class DeadLock {

    private AtomicInteger  a  = new AtomicInteger(0);

    private final  String read_lock = new String();

    private final String write_lock = new String();


    public static void main(String[] args) {
        DeadLock deadLock = new DeadLock();

        new Thread(()->{
            while (true){
                deadLock.read();
            }
        },"read-first-thread").start();


        new Thread(()->{
            while (true){
                deadLock.write();
            }
        },"write-first-thread").start();

//        String  name  = "中国2eeee和你%￥￥￥&**（@##*#！）（*&……%￥#！";
//        System.out.println(name.toUpperCase());

    }


    public void read() {
        synchronized (read_lock) {
            System.out.println(Thread.currentThread().getName() + " got read lock and then i want to write");
            synchronized (write_lock) {
                System.out.println(Thread.currentThread().getName() + " got read lock and write lock");
            }
        }
    }

    public void write() {
        synchronized (write_lock) {
            System.out.println(Thread.currentThread().getName() + " got write lock and then i want to read");
            synchronized (read_lock) {
                System.out.println(Thread.currentThread().getName() + " got write lock and read lock");
            }
        }
    }

}
